package com.formina.stars;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.formina.engine.Engine;
import com.formina.engine.Model;



public class Star extends Model {

		public static int GROWING = 0;
		public static int IDLE = 1;
		public static int ANIMATING = 2;
		
		private int rotationAxis = 0;
		private int mode = 0;
		private float[] growingScale;
		
		private long[] animations = {0,100,200,300,400,500};
		private long animationStart = 0;
		Random rand = new Random();
	 
       public Star(float[] location, float[] scale){
    	   super(location, scale, Engine.Star);
    	   //this.setScale(new float[]{50f,50f,50f});
    	   growingScale = new float[]{.01f,.01f,.01f};
    	   rotationAxis = 1;//r.nextInt(3);
    	   this.setRandomColor();
    	   for(long l :animations){
    		   l = l*14;
    	   }
    	   
       }
	   
	   public Star() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   @Override
	   public void update(long time){
		   
		   float growrate = .10f;
		 
		   if(this.addedTime==0){
			   addedTime = time;
			   mode = 0;  //growing
		   }
		   switch(mode){
		   		case 0:
		   			if(this.Scale()[0]>this.growingScale[0]&&this.Scale()[1]>this.growingScale[1]&&this.Scale()[2]>this.growingScale[2]){
		 			   growrate = .10f*this.Scale()[0];
		 			   this.growingScale[0]+=growrate;
		 			   this.growingScale[1]+=growrate;
		 			   this.growingScale[2]+=growrate;
		 		   }else{
		 			   mode = 1; //now we're done growing.  idle
		 			   this.growingScale = this.Scale().clone();
		 		   }
		 		   break;
		   		case 1:
		   			if(rand.nextInt(600)<1){
		   				mode = 2;
		   				break;
		   			}
		   			break;
		   		case 2:
		   			growrate=.05f;
		   			if(animationStart ==0){
		   				lastTime = 0;
		   				animationStart = time;
		   				break;
		   			}
		   			
		   			long animTime = lastTime-animationStart;
		   			
		   			if(animTime>=this.animations[0] && animTime <=this.animations[1]){
		   				this.growingScale = this.Scale().clone();
		   			}else if(animTime>=this.animations[1] && animTime <=this.animations[2]){
		   				this.growingScale[0]+=growrate;
			 			this.growingScale[1]+=growrate;
			 			this.growingScale[2]+=growrate;
		   			}else if(animTime>=this.animations[2] && animTime <=this.animations[3]){
		   				this.growingScale[0]-=growrate;
			 			this.growingScale[1]-=growrate;
			 			this.growingScale[2]-=growrate;
		   			}else if(animTime>=this.animations[3] && animTime <=this.animations[4]){
		   				this.growingScale[0]+=growrate;
			 			this.growingScale[1]+=growrate;
			 			this.growingScale[2]+=growrate;
		   			}else if(animTime>=this.animations[4] && animTime <=this.animations[5]){
		   				this.growingScale[0]-=growrate;
			 			this.growingScale[1]-=growrate;
			 			this.growingScale[2]-=growrate;
		   			}else{
		   				//animation is over
		   				this.growingScale = Scale().clone();
		   				animationStart = 0;
		   				mode = 1;
		   			}	
		   			break;
		   }
		   this.Rotation()[rotationAxis]+=.5f;
 		   if(this.Rotation()[rotationAxis]>360){
 			   this.Rotation()[rotationAxis]=1;
 		   }
		   lastTime = time;
		   
	   }
	   
	   @Override
	   public void setIsDead(boolean isDead){
		   super.setIsDead(isDead);
		   
	   }
	   
	   @Override
	    public void draw(GL10 gl) {

			
	        
	     // Use culling to remove back faces.
	        GLES20.glEnable(GLES20.GL_CULL_FACE);
	        GLES20.glBlendFunc (GLES20.GL_ONE, GLES20.GL_ZERO);	         

	        // Enable depth testing
	        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
	        
	        Matrix.setIdentityM(mModelMatrix, 0);
	        Matrix.setIdentityM(rotationMatrix, 0);
	        Matrix.setIdentityM(scaleMatrix, 0);
	        Matrix.translateM(mModelMatrix, 0, this.getX(), this.getY(), this.getZ());
	        Matrix.scaleM(mModelMatrix, 0, growingScale[0], growingScale[1],growingScale[2]);
	        Matrix.rotateM(mModelMatrix, 0, this.Rotation()[this.rotationAxis], 0f, 0f, 1f);

	        
	        GLES20.glVertexAttrib3fv(Engine.mLightPositionHandle, new float[] {getX()-50, getY()+100, getZ()+100}, 0);
	        
	        //position
	        modelInfo.vertexBuffer.position(0);
	        GLES20.glVertexAttribPointer(Engine.mPositionHandle, 3, GLES20.GL_FLOAT, false,
	        		0, modelInfo.vertexBuffer);
			GLES20.glEnableVertexAttribArray(Engine.mPositionHandle);
	        //end position
			
			//color
			
			if(Engine.UseRandomColors){
		        GLES20.glVertexAttrib4f(Engine.mColorHandle, this.Color()[0],this.Color()[1],this.Color()[2],this.Color()[3]);
			}else{
		        GLES20.glVertexAttrib4f(Engine.mColorHandle, Engine.DefaultColor[0],Engine.DefaultColor[1],Engine.DefaultColor[2],Engine.DefaultColor[3]);

			}

			//GLES20.glVertexAttribPointer(Engine.mColorHandle, 3, GLES20.GL_FLOAT, false,
	        		//0, this.ColorBufferV());    
	        //GLES20.glEnableVertexAttribArray(Engine.mColorHandle);
			//end color
	        
	        //normals for lighting
	        GLES20.glVertexAttribPointer(Engine.mNormalHandle, 3, GLES20.GL_FLOAT, false, 
	        		0, this.modelInfo.normalBuffer);
	        GLES20.glEnableVertexAttribArray(Engine.mNormalHandle);

	        //end normals
				       	        
	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mVMatrix, 0, mModelMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVMatrixHandle, 1, false, Engine.mMVPMatrix, 0);

	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mProjMatrix, 0, Engine.mMVPMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVPMatrixHandle, 1, false, Engine.mMVPMatrix, 0);
	        
	        // Pass in the light position in eye space.        
	        GLES20.glUniform3f(Engine.mLightPosHandle, Engine.mLightPosInEyeSpace[0], Engine.mLightPosInEyeSpace[1], Engine.mLightPosInEyeSpace[2]);
	        
	        //color
	        //GLES20.glUniform4fv(Engine.mColorHandle, 1, this.ColorBuffer());
	        //end color
	        
	        GLES20.glDrawElements(GLES20.GL_TRIANGLES, modelInfo.Indices.length,// OpenGL docs
					GLES20.GL_UNSIGNED_SHORT, modelInfo.indexBuffer);
	        
	        GLES20.glDisable(GLES20.GL_CULL_FACE);
	        GLES20.glDisable(GLES20.GL_DEPTH_TEST);
	        				
	   }
	   
	}

